// pages/bloodsugarrecord/bloodsugarrecord.js
var app = getApp();
Page({
    options: {
        styleIsolation: 'apply-shared',
      },
    /**
     * 页面的初始数据
     */
    data: {
        priceError1: false,
        priceError2: false,
        priceError3: false,
        priceError4: false,
        highSugar: 0,
        lowSugar: 0,
        scanSugar: 0,
        fingerSugar: 0.0,
        type:'',
        recordTime: 0,
        mode: '',
        minute: '23:59',
        types: [
            { label: '空腹', value: '空腹' },
            { label: '早餐后', value: '早餐后' },
            { label: '上午加餐', value: '上午加餐' },
            { label: '午餐前', value: '午餐前' },
            { label: '午餐后', value: '午餐后' },
            { label: '下午加餐', value: '下午加餐' },
            { label: '晚餐前', value: '晚餐前' },
            { label: '晚餐后', value: '晚餐后' },
            { label: '晚间加餐', value: '晚间加餐' },
            { label: '睡前', value: '睡前' },
            { label: '夜间', value: '夜间' }
          ],
    },
    onPriceInput1(e) {
        const { priceError1 } = this.data;
        const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
        if (priceError1 === isNumber) {
          this.setData({
            priceError1: !isNumber,
          });
        }
    },
    onPriceInput2(e) {
        const { priceError2 } = this.data;
        // console.log("1:",priceError2);
        const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
        if (priceError2 === isNumber) {
          this.setData({
            priceError2: !isNumber,
          });
        } 
    },
    onPriceInput3(e) {
        const { priceError3 } = this.data;
        const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
        if (priceError3 === isNumber) {
          this.setData({
            priceError3: !isNumber,
          });
        }
    },    
    onPriceInput4(e) {
        const { priceError4 } = this.data;
        const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
        if (priceError4 === isNumber) {
          this.setData({
            priceError4: !isNumber,
          });
        }
        if(isNumber === true) {
            // console.log("2:",parseFloat(e.detail.value));
            // console.log(typeof parseFloat(e.detail.value));
            this.setData({
                fingerSugar: parseFloat(e.detail.value)
            })
        } else {
            this.setData({
                fingerSugar: e.detail.value
            })
        }
    },
    showPicker(e) {
        const { mode } = e.currentTarget.dataset;
        this.setData({
          mode,
          [`${mode}Visible`]: true,
        });
      },
      hidePicker() {
        const { mode } = this.data;
        this.setData({
          [`${mode}Visible`]: false,
        });
      },
      onConfirm(e) {
        const { value } = e.detail;
        const { mode } = this.data;
        // console.log('confim', value);

        this.setData({
          [mode]: value,
          [`${mode}Text`]: value,
          recordTime: Date.parse(new Date(new Date().setHours(parseInt(value.toString().substr(0,2)),parseInt(value.toString().substr(3,2)),0)))/1000
        });
        console.log("recordTime:",this.data.recordTime);
        this.hidePicker();
      },
  
      onColumnChange(e) {
        // console.log('pick', e.detail.value);
      },

      onColumnChange1(e) {
        // console.log('picker pick:', e);
      },
  
      onPickerChange(e) {
        const { key } = e.currentTarget.dataset;
        const { value } = e.detail;
  
        // console.log('picker change:', e.detail.value[0]);
        this.setData({
          [`${key}Visible`]: false,
          [`${key}Value`]: value,
          [`${key}Text`]: value.join(' '),
          type: e.detail.value[0]
        });
      },
  
      onPickerCancel(e) {
        const { key } = e.currentTarget.dataset;
        // console.log(e, '取消');
        // console.log('picker1 cancel:');
        this.setData({
          [`${key}Visible`]: false,
        });
      },
  
      onTypePicker() {
        this.setData({ typeVisible: true });
      },

      submit: function() {
        wx.request({ // 调用接口
            url: `${app.globalData.protocol}://${app.globalData.host}/bgmp/api/user?Action=AddBg`,          
            method: 'POST',
            header: {
              "Content-Type": "application/json",
              "x-api-key": wx.getStorageSync('token')
            },
            data: {
                "time": this.data.recordTime,
                "value": this.data.fingerSugar,
                "type": this.data.type
            },
            success:(res) => { // 接口调用成功，获取token并缓存
              console.log(res);
              if(res.statusCode == 200) {
                console.log("submit successfully");
                wx.showModal({
                    title: '上传成功',
                    confirmText: "确定",
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log("aaaaaa");
                        //点击确定按钮
                        wx.navigateBack();
                      } else if (res.cancel) {
                        //点击取消按钮
                      }
                    }
                })
              } else {
                console.log("submit unsuccessfully");
                wx.showModal({
                    title: '上传失败',
                    confirmText: "确定",
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log("确定");
                      } else if (res.cancel) {
                        //点击取消按钮
                        console.log("取消");
                      }
                    }
                })
              }
            },
            fail() {
              console.log("submit unsuccessfully");
            }
        });  
        wx.request({ // 调用接口
            url: `${app.globalData.protocol}://${app.globalData.host}/bgmp/api/user?Action=ListTags`,
            // url: 'https://124.220.110.138:443/bgmp/api/user?Action=ListTags',           
            method: 'POST',
            header: {
              "Content-Type": "application/json",
              "x-api-key": wx.getStorageSync('token')
            },
            data: {
            },
            success:(res) => { // 接口调用成功，获取token并缓存
              console.log("tags:");
              console.log(res);
              if(res.statusCode == 200) {
                console.log("submit successfully");
              } else {
                console.log("submit unsuccessfully");
              }
            },
            fail() {
              console.log("submit unsuccessfully");
            }
        });    
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            minute:new Date().getHours().toString()+':'+new Date().getMinutes().toString()
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // wx.navigateTo({
        //     url: '/pages/bloodsugar/bloodsugar',
        //   })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})